package cn.edu.ldu.views.login;

import cn.edu.ldu.entity.UserEntity;
import cn.edu.ldu.protocol.Protocol;
import cn.edu.ldu.util.Consts;
import cn.edu.ldu.util.L;
import cn.edu.ldu.util.Translate;
import cn.edu.ldu.views.controller.MainUIController;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.digest.DigestUtils;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.*;

/**
 *
 * @author DYZ
 */
public class EditPassUI extends javax.swing.JPanel {

    /**
     * Creates new form EditPassUI
     */
    public EditPassUI() {
        initComponents();
        this.setSize(Consts.L_CONTENT_WIDTH,Consts.L_CONTENTS_HEIGHT_OPEN);
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        btnPanel = new javax.swing.JPanel();
        btnReset = new javax.swing.JButton();
        btnSubmit = new javax.swing.JButton();
        mainPanel = new javax.swing.JPanel();
        labUserId = new javax.swing.JLabel();
        txtUserId = new javax.swing.JTextField();
        labUserMobile = new javax.swing.JLabel();
        txtUserMobile = new javax.swing.JTextField();
        labNewPass = new javax.swing.JLabel();
        txtNewPass = new javax.swing.JTextField();
        labSurePass = new javax.swing.JLabel();
        txtSurePass = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createTitledBorder("修改密码"));
        setLayout(new java.awt.BorderLayout());

        btnReset.setText("重置");
        btnReset.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnResetMouseClicked(evt);
            }
        });

        btnSubmit.setText("确定修改");
        btnSubmit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSubmitMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout btnPanelLayout = new javax.swing.GroupLayout(btnPanel);
        btnPanel.setLayout(btnPanelLayout);
        btnPanelLayout.setHorizontalGroup(
                btnPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(btnPanelLayout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                                .addComponent(btnSubmit)
                                .addGap(100, 100, 100))
        );
        btnPanelLayout.setVerticalGroup(
                btnPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(btnPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(btnPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnReset)
                                        .addComponent(btnSubmit))
                                .addContainerGap(23, Short.MAX_VALUE))
        );

        add(btnPanel, java.awt.BorderLayout.PAGE_END);

        labUserId.setText("账户：");

        labUserMobile.setText("手机号码：");

        labNewPass.setText("新密码：");

        labSurePass.setText("确定密码：");

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addGroup(mainPanelLayout.createSequentialGroup()
                                                        .addComponent(labNewPass)
                                                        .addGap(18, 18, 18)
                                                        .addComponent(txtNewPass, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))
                                                .addGroup(mainPanelLayout.createSequentialGroup()
                                                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                .addComponent(labUserMobile)
                                                                .addComponent(labUserId))
                                                        .addGap(18, 18, 18)
                                                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                                .addComponent(txtUserId)
                                                                .addComponent(txtUserMobile, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))))
                                        .addGroup(mainPanelLayout.createSequentialGroup()
                                                .addComponent(labSurePass)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtSurePass, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(52, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(labUserId)
                                        .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(26, 26, 26)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(labUserMobile)
                                        .addComponent(txtUserMobile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 28, Short.MAX_VALUE)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(labNewPass)
                                        .addComponent(txtNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(24, 24, 24)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(labSurePass)
                                        .addComponent(txtSurePass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(19, 19, 19))
        );

        add(mainPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>

    /**
     * 重置密码
     * 将所有的输入框全部都设置为空
     */
    private void btnResetMouseClicked(MouseEvent evt){
        txtUserId.setText("");
        txtUserMobile.setText("");
        txtNewPass.setText("");
        txtSurePass.setText("");
    }

    /**
     * 提交修改密码结果
     * 1、需要判定是否为空
     * 2、在不为空的情况下，需要判定新密码和确定密码一致
     * 3、进行账户查找然后进行修改
     */
    private void btnSubmitMouseClicked(MouseEvent evt){
        //判断是否为空
        if((txtUserId.getText().trim().equals(""))||(txtUserMobile.getText().trim().equals(""))||(txtNewPass.getText().trim().equals(""))||(txtSurePass.getText().trim().equals(""))){
            JOptionPane.showMessageDialog(null, "请将信息填写完整", "提示", JOptionPane.ERROR_MESSAGE);
        }else{
            //判定新密码和确定密码是否一致
            if (txtNewPass.getText().trim().equals(txtSurePass.getText().trim())){
                //询问是否继续操作
                int res = JOptionPane.showConfirmDialog(null, "确定信息是否无误", "是否继续", JOptionPane.YES_NO_OPTION);
                if (res == JOptionPane.YES_OPTION) {
                    //提交给服务器，进行查找
                    String userId = txtUserId.getText().trim();
                    String userMobile = txtUserMobile.getText().trim();
                    String userPass = DigestUtils.sha256Hex(txtNewPass.getText().trim());

                    //提交到服务器
                    InetAddress remoteAddr = null;
                    try {
                        remoteAddr=InetAddress.getByName(Consts.ipv4);
                        //创建UDP套件字
                        DatagramSocket clientSocket = null;
                        clientSocket = new DatagramSocket();
                        clientSocket.setSoTimeout(5000);

                        //构造用户修改密码信息
                        Protocol protocol = new Protocol();
                        protocol.setKey("EditPassword");
                        UserEntity ue = new UserEntity();
                        ue.setId(Long.valueOf(userId));
                        ue.setPassword(userPass);
                        ue.setMobile(userMobile);

                        //转字符
                        protocol.setData(JSON.toJSONString(ue));
                        byte[] data = Translate.ObjectToByte(protocol);

                        //定义修改密码报文
                        DatagramPacket packet = new DatagramPacket(data,data.length,remoteAddr,Consts.port);

                        //发送报文
                        clientSocket.send(packet);
                        L.OK(packet,EditPassUI.class);

                        //从服务器得到令牌
                        byte[] backData = new byte[8096];
                        DatagramPacket backPacket = new DatagramPacket(backData,backData.length,remoteAddr,Consts.port);

                        clientSocket.receive(backPacket);
                        L.OK(backPacket,EditPassUI.class);

                        Protocol pro = (Protocol) Translate.ByteToObject(backPacket.getData());
                        //解决视图问题


                        JOptionPane.showMessageDialog(this,pro.getKey(),"修改结果",JOptionPane.INFORMATION_MESSAGE);


                    } catch (IOException  e) {
                        e.printStackTrace();
                    }
                    //将输入框全部清零
                    txtUserId.setText("");
                    txtUserMobile.setText("");
                    txtNewPass.setText("");
                    txtSurePass.setText("");
                } else {
                    return;
                }
            }else{
                JOptionPane.showMessageDialog(null, "请将新密码和确定密码输入一致", "提示", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel btnPanel;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel labNewPass;
    private javax.swing.JLabel labUserMobile;
    private javax.swing.JLabel labSurePass;
    private javax.swing.JLabel labUserId;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTextField txtNewPass;
    private javax.swing.JTextField txtUserMobile;
    private javax.swing.JTextField txtSurePass;
    private javax.swing.JTextField txtUserId;
    // End of variables declaration
}
